
/**
 * amind组件接口初始化
 */
function init() {
    amind.init("container", { "appId": "AAAAA-BBBBB-CCCCC-DDDDD", "access_token": "qwezxzsddew" });

    // 绑定amind事件
    // 1. 初始化完成
    amind.on("ready", function () {
        $("#txtResult").html("准备就绪");
    });

    // 2. 激活主题
    amind.on("topicActive", function (argument) {
        $("#txtResult").html($("#txtResult").html() + "\n" + JSON.stringify(argument));
        // 滚动到底部
        $("#txtResult").scrollTop($("#txtResult")[0].scrollHeight);
    });

    // 3.点击主题中的链接
    amind.on("linkActive", function (argument) {
        $("#txtResult").html($("#txtResult").html() + "\n" + JSON.stringify(argument));
        // 滚动到底部
        $("#txtResult").scrollTop($("#txtResult")[0].scrollHeight);
    });
}

// 数据操作
async function doDataManager(button) {
    let text = $(button).text();
    if (text === "装载数据") {
        let inputText = $("#txtContent").val();
        try {
            if (inputText.trim() == "") {
                _showErrorMsg("数据不能为空");
            } else {
                await amind.loadGeomData({ "geomData": JSON.parse(inputText) });
            }
        } catch (e) {
            _showErrorMsg("GeomData数据错误");
        }
    } else if (text === "清空数据") {
        await amind.clearData();
    } else if (text === "获取数据") {
        let result = await amind.getGeomData();
        $("#txtResult").html(JSON.stringify(result));
    } else if (text === "获取选中主题数据") {
        let result = await amind.getTopic();
        $("#txtResult").html(JSON.stringify(result));
    } else if (text === "居中主题") {
        await amind.showTopic("G00006QE")
    } else if (text === "获取文档修改状态") {
        let status = await amind.getFileSaveStatus();
        alert(status === 1 ? "已修改" : (status === 2 ? "未修改" : "未修改"));
    }
}

let num = 1;

// 主题操作
async function doTopicManager(button) {
    let text = $(button).text();
    let result;
    if (text === "增加子主题") {
        result = await amind.addTopic("子主题" + num, "child", { "type": "child", "data": num })
        num++
    } else if (text === "增加父主题") {
        result = await amind.addTopic("主题" + num, "father", { "type": "father", "data": num })
        num++
    } else if (text === "增加同级主题(弟)") {
        result = await amind.addTopic("主题" + num, "brother", { "type": "brother", "data": num })
        num++
    } else if (text === "增加同级主题(兄)") {
        result = await amind.addTopic("主题" + num, "elderBrother", { "type": "eld", "data": num })
        num++
    } else if (text === "删除主题") {
        result = await amind.deleteTopic()
    } else if (text === "展开") {
        result = await amind.expandTopic();
    } else if (text === "折叠") {
        result = await amind.collapseTopic();
    } else if (text === "增加主题链接") {
        result = await amind.addTopicLink("链接至系统", { "url": "000113113" });
    }
    console.info("doTopicManager", result);
}

// 导入导出
async function doExportData(button) {
    let text = $(button).text();
    if (text === "新建思维导图") {
        await amind.createMind();
    } else if (text === "导入数据") {
        let inputText = $("#txtContent").val().trim();
        if (inputText.trim() == "") {
            _showErrorMsg("数据不能为空")
        } else {
            if (inputText.substring(0, 1) === "{") {
                // json格式
                await amind.importData(inputText, { "fileType": "json" });
            } else {
                // txt或md格式
                await amind.importData(inputText);
            }
        }
    } else if (text === "导出为图像数据") {
        await _exportData("image");
    } else if (text === "导出为json数据") {
        await _exportData("json");
    } else if (text === "导出为txt数据") {
        await _exportData("txt");
    } else if (text === "导出为md数据") {
        await _exportData("md");
    } else if (text === "清空数据") {
        await amind.clearData();
    }
}

// 图形布局
async function doSetLayout(button) {
    let text = $(button).text();
    if (text === "逻辑图+向右") {
        await amind.setLayout("leveltreec", "LR")
    } else if (text === "逻辑图+向左") {
        await amind.setLayout("leveltreec", "RL")
    } else if (text === "树状+右下") {
        await amind.setLayout("leveltree", "TBR")
    } else if (text === "树状+左下") {
        await amind.setLayout("leveltree", "TBL")
    } else if (text === "时间线+向右") {
        await amind.setLayout("timeline", "LR")
    } else if (text === "时间线+向下") {
        await amind.setLayout("timeline", "RL")
    } else if (text === "树型表格") {
        await amind.setLayout("treeform", "TTB")
    } else if (text === "树型矩阵") {
        await amind.setLayout("treematrix", "MLR")
    } else if (text === "居中显示") {
        await amind.centerView();
    }
}

// 风格样式
async function doSetTheme(button) {
    let text = $(button).text();
    if (text === "风格: 经典") {
        await amind.applyTheme("经典")
    } else if (text === "风格: 流行") {
        await amind.applyTheme("流行")
    } else if (text === "风格: 梦幻") {
        await amind.applyTheme("梦幻")
    } else if (text === "风格: 活力") {
        await amind.applyTheme("活力")
    } else if (text === "风格: 复古") {
        await amind.applyTheme("复古")
    } else if (text === "风格: 正式") {
        await amind.applyTheme("正式")
    } else if (text === "风格: 深沉") {
        await amind.applyTheme("深沉")
    } else if (text === "风格: 淡雅") {
        await amind.applyTheme("淡雅")
    }
}

// 界面控制
async function doSetFace(button) {
    let panelNames = [];
    $("#toolbar").find(".part2").find("input[type='checkbox']").each((idx, ele) => {
        let tag = $(ele).attr("data-tag");
        let check = $(ele).prop("checked");
        if (check) {
            panelNames.push(tag);
        }
    })
    let text = $(button).text();
    if (text === "显示") {
        await amind.showPanel(panelNames)
    } else if (text === "隐藏") {
        await amind.hidePanel(panelNames)
    }
}

async function doSubgraph(button) {
    let text = $(button).text();
    if (text === "插入子图") {
        let inputText = $("#txtContent").val();
        try {
            if (inputText.trim() == "") {
                _showErrorMsg("数据不能为空");
            } else {
                await amind.insertSubgraph({ "geomData": JSON.parse(inputText) });
            }
        } catch (e) {
            _showErrorMsg(e.message);
        }
    } else if (text === "缓存子图数据") {
        try {
            let inputText = $("#txtContent").val();
            if (inputText.trim() == "") {
                _showErrorMsg("数据不能为空");
            } else {
                await amind.addRefSubgraphData({ "name": "myRefrenceGraph", "geomData": JSON.parse(inputText) });
            }
        } catch (e) {
            _showErrorMsg(e.message);
        }
    } else if (text === "引用子图") {
        try {
            await amind.insertRefSubgraph({ "name": "myRefrenceGraph" });
        } catch (e) {
            _showErrorMsg(e.message);
        }
    }
}

// 显示错误信息
function _showErrorMsg(msg) {
    $("#txtResult").html($("#txtResult").html() + "\n" + msg)
    alert(msg)
}

// 导出数据
async function _exportData(type) {
    let result = await amind.getGeomData();
    if (result.layers[0].data.length > 0) {
        result = await amind.exportData(type)
        if (type === "json") {
            $("#txtResult").html(JSON.stringify(result));
        } else {
            $("#txtResult").html(result);
        }
    } else {
        _showErrorMsg("数据不能为空")
    }
    return result;
}
